package com.mx.kn.cstf2.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mx.kn.cstf2.dao.ImplementationDao;
import com.mx.kn.cstf2.dao.TransitionDao;
import com.mx.kn.cstf2.dts.FilesUploadDts;
import com.mx.kn.cstf2.dts.JoindocumenttransitionDts;
import com.mx.kn.cstf2.dts.PersonOptions;

import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.transition.Bean.AsignIfownerStkBean;
import kn.cstf2.transition.Bean.JoinPersonTransitionBean;
import kn.cstf2.transition.Bean.Localidad;
import kn.cstf2.transition.Bean.PersonalBean;
import kn.cstf2.transition.Bean.T_clientesDataFromStkBean;
import kn.cstf2.transition.Bean.TransitionBean;

import com.mx.kn.cstf2.dto.HistoricoDto;
import com.mx.kn.cstf2.dto.LinksDto;
import com.mx.kn.cstf2.dto.UsuarioDto;
import com.mx.kn.cstf2.service.TransitionService;

@Service
public class TransitionServiceImpl implements TransitionService {

	@Autowired TransitionDao transitionDao;
	
	public ArrayList<String> getListaNombreDePersonal() throws SQLException {
		return transitionDao.getListaNombreDePersonal();
	}

	
	public TransitionBean getTransitionBeanIdKnkey(TransitionBean transitionBean) throws SQLException {
		
		return transitionDao.obtenerTransitionBeanByKnkeyAndId(transitionBean);
	}

	
	public FilesUploadDts verifyUploaded(TransitionBean transition) throws SQLException {
		return transitionDao.verifyUploaded(transition);
	}

	
	public TransitionBean getJoindocumenttransitionDetail(int RATE_TRACKER, int ACON_DEBTOR_NUMBER,
			Integer idtransition) throws SQLException {
		
		return transitionDao.obtenerJoindocumenttransitionDetail(RATE_TRACKER, ACON_DEBTOR_NUMBER, idtransition);
	}

	
	public String getTransitionLog(TransitionBean transition) throws SQLException {
		return transitionDao.obtenerTransitionLog(transition);
	}

	
	public void updateLogEnTransition(TransitionBean transitionBean)
			throws SQLException {
		if (transitionBean != null) {
			 transitionDao.actualizarLogEnTransition(transitionBean);
		} else {
			throw new CstF2Exception("El Usuario es null");
		}

	}

	
	public T_clientesDataFromStkBean getDataFromSTK(
			TransitionBean transitionBean) throws SQLException {
		 T_clientesDataFromStkBean t_clientesDataFromStkBean = new T_clientesDataFromStkBean();
	        if (t_clientesDataFromStkBean != null) {
	            t_clientesDataFromStkBean = transitionDao.obtenerDataFromTClientesSTK(transitionBean);
	        } else {
	            throw new CstF2Exception("El Usuario es null");
	        }
	        return t_clientesDataFromStkBean;
	}

	
	public AsignIfownerStkBean getIfOwnerFromSTK(TransitionBean transitionBean)
			throws SQLException {
		  AsignIfownerStkBean asignIfownerStkBean = new AsignIfownerStkBean();
	        if (asignIfownerStkBean != null) {
	            asignIfownerStkBean = transitionDao.obtenerIfOwnerFromSTK(transitionBean);
	        } else {
	            throw new CstF2Exception("El Usuario es null");
	        }
	        return asignIfownerStkBean;
	}

	
	public JoindocumenttransitionDts getVoBoInJoindocumenttransition(
			Integer idtransition) throws SQLException {
		JoindocumenttransitionDts joindocumenttransitionBean = new JoindocumenttransitionDts();

        if (idtransition != null) {
            joindocumenttransitionBean = transitionDao.selectVoBoEnJoindocumenttransition(idtransition);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
        return joindocumenttransitionBean;
	}

	
	 public ArrayList<PersonalBean> getDataPersonalByIdPersonal(Integer idp)
			throws SQLException {
		return transitionDao.obtenerDataPersonalByIdPersonal(idp);
	}

	
	public ArrayList<Localidad> branch() throws SQLException {
		return transitionDao.branch();
	}

	
	public String getLocality(Integer knkey) throws SQLException {
		return transitionDao.getLocality(knkey);
	}

	
	public String validateBranch(Integer knkey) throws SQLException {
		return transitionDao.validateBranch(knkey);
	}

	
	public JoindocumenttransitionDts getJoindocumenttransitionData(
			TransitionBean transitionBean) throws SQLException {

		return transitionDao.obtenerJoindocumenttransitionData(transitionBean);

	}

	
	public Map<Integer,Boolean> checkVoBoCheckbox(
			JoindocumenttransitionDts joindocumenttransitionBean) throws SQLException {
            return transitionDao.checkCheckbox(joindocumenttransitionBean);
        
		
	}
	
	public void updateCheckbox(JoindocumenttransitionDts joindocumenttransitionBean, int iddocument) throws SQLException{
		transitionDao.updateCheckbox(joindocumenttransitionBean, iddocument);
	}

	
	public Integer verificarCountJoinPersontransition(
			JoinPersonTransitionBean joinPersonTransitionBean)
			throws SQLException {
        if (joinPersonTransitionBean != null) {
            return transitionDao.verificarCountJoinusertransition(joinPersonTransitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
  
	}

	
	public void deleteRelationJoinusertransitionByIdPerson(Integer idperson,
			Integer idtransition) throws SQLException {
		  if (idperson != null) {
			  transitionDao.eliminarRelacionJoinPersontransitionByIdPerson(idperson, idtransition);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
		
	}

	
	public void deletePersonalById(PersonalBean personalBean)
			throws SQLException {
      if (personalBean != null) {
    	  transitionDao.eliminarPersonalById(personalBean);
      } else {
    	  throw new CstF2Exception("El Usuario es null");
      }
		
	}

	
	public void setJoinPersonTransition(PersonalBean personalBean,
			TransitionBean transitionBean) throws SQLException {
      //JoinPersonTransitionBean   joinPersonTransitionBean = new   JoinPersonTransitionBean();
      if (personalBean != null) {
    	  transitionDao.insertarJoinPersonTransition(personalBean, transitionBean);
      } else {
          throw new CstF2Exception("El Usuario es null");
      }
      //return joinPersonTransitionBean;
		
	}

	
	public PersonalBean insertDataPersonal(PersonalBean personalBean)
			throws SQLException {
		if (personalBean != null) {
			return transitionDao.insertarDataPersonal(personalBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
		
	}

	
	public PersonalBean getDataPersonalByEmail(String mail) throws SQLException {

        return  transitionDao.obtenerDataPersonalByEmail(mail);

	}

	
	public JoindocumenttransitionDts updateVoBoInJoindocumenttransition(
			JoindocumenttransitionDts joindocumenttransitionBean, String fecha)
			throws SQLException {
		 if (joindocumenttransitionBean != null) {
	           return transitionDao.actualizarVoBoEnJoindocumenttransition(joindocumenttransitionBean, fecha);
	        } else {
	            throw new CstF2Exception("El Usuario es null");
	        }
	        
	}

	
	public TransitionBean updateStatusEnTransition(TransitionBean transitionBean,Date date, String userName)
			throws SQLException {
		 if (transitionBean != null) {
	            return transitionDao.actualizarStatusEnTransition(transitionBean,date, userName);
	        } else {
	            throw new CstF2Exception("El Usuario es null");
	        }
	}

	
	public TransitionBean updateStatusEnImplementation(
			TransitionBean transitionBean, String userName) throws SQLException {
        if (transitionBean != null) {
            return transitionDao.actualizarStatusEnImplementation(transitionBean, userName);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }

	}

	
	public PersonalBean getDataPersonal(TransitionBean transitionBean)
			throws SQLException {
	    PersonalBean personalBean = new PersonalBean();
        if (personalBean != null) {
            return transitionDao.obtenerDataPersonal(transitionBean);
        } else {
            throw new CstF2Exception("El Usuario es null");
        }
	}

	
	public PersonalBean getDataPersonalByName(String name) throws SQLException {
		return transitionDao.obtenerDataPersonalByName(name);

	}

	
	public ArrayList<PersonalBean> getDataPersonalByKnkey(Integer knkey)
			throws SQLException {
		 
	     return transitionDao.obtenerDataPersonalByKnkey(knkey);   
	     
	}


	
	public PersonalBean getDataPersonalById(Integer idp) throws SQLException {

		return transitionDao.obtenerDataPersonalById(idp);

	}


	@Override
	public TransitionBean getLastDateUpdate(TransitionBean transitionBean)
			throws SQLException {
		return transitionDao.getLastDateUpdate(transitionBean);

	}


	@Override
	public TransitionBean insertLastDateUpdateComment(TransitionBean transitionBean, Date date, String userName)
			throws SQLException {
		 return transitionDao.insertLastDateUpdateComment(transitionBean,date, userName);
		
	}


	@Override
	public TransitionBean getLastDateUpdateComment(TransitionBean transitionBean)
			throws SQLException {
		return transitionDao.getLastDateUpdateComment(transitionBean);
	}


	@Override
	public void updateDetailsInJoindocumenttransition(String detailsnum,
			Integer documentid, Integer transitionId) throws SQLException {
		transitionDao.actualizarDetailsEnJoindocumenttransition(detailsnum, documentid, transitionId);
		
	}


	@Override
	public TransitionBean actualizarlastDateUpdateRequeri(Integer id,
			Integer knkey, String userName) throws SQLException {
		return transitionDao.actualizarlastDateUpdateRequeri(id, knkey, userName);
		
	}


	@Override
	public TransitionBean getLastDateUpdateRequi(TransitionBean transitionBean)
			throws SQLException {
		return transitionDao.getLastDateUpdateRequi(transitionBean);
	}


	@Override
	public boolean checkIfExistTransaction(int knkey) throws SQLException {
		return transitionDao.checkIfExistTransaction(knkey);
	}


	@Override
	@Transactional
	public void newTransition(int knKey, String companyName,  UsuarioDto usuario)
			throws SQLException {
		transitionDao.newTransition(knKey, usuario);

		//se debe mandar mail?
//      sendEmail("" + KnKey, companyName);
	}


	@Override
	public void updateUploadedFile(int idFile, int idTransaction, String detail)
			throws SQLException {
		transitionDao.updateUploadedFile(idFile, idTransaction, detail);	
	}


	@Override
	public JoindocumenttransitionDts getJoinDocumentTransaction(int idDocument,
			int idTransition) throws SQLException {
		return transitionDao.getJoinDocumentTransaction(idDocument, idTransition);
	}


	@Override
	public void updateJoinTransitionDocuments(Map<Integer, Boolean> checkBoxs,
			int idTransition) throws SQLException {
		transitionDao.updateJoinTransitionDocuments(checkBoxs, idTransition);
		
	}


	@Override
	public void insertHistorico(HistoricoDto historico) throws SQLException {
		
		transitionDao.insertHistorico(historico);
	}


	@Override
	public List<JoindocumenttransitionDts> getEstatusAndDateDocuments(
			TransitionBean transition) throws SQLException {
		return transitionDao.getEstatusAndDateDocuments(transition);
	}


	@Override
	public List<PersonOptions> getPersonalAdd(TransitionBean transition)
			throws SQLException {
		
		return transitionDao.getPersonalAdd(transition);
	}


	@Override
	public TransitionBean setLastDateUpdateSubmitFileImplementation(
			TransitionBean transition, Date date, String userName)
			throws SQLException {

		return transitionDao.setLastDateUpdateSubmitFileImplementation(transition, date, userName);
	}


	
	
	
	
	
	
	
}
